package au.com.iglootech.component;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;

import au.com.iglootech.TaxTimeToolTip.ui.invoice.PerviewWindow;

import com.itextpdf.text.Document;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import com.itextpdf.text.pdf.PdfWriter;
import com.vaadin.terminal.StreamResource;
import com.vaadin.terminal.StreamResource.StreamSource;

public class IGTTemplatePDF implements StreamResource.StreamSource {
    private final transient ByteArrayOutputStream os = new ByteArrayOutputStream();
    
    private static final String ADDRESS = "Address";
    private static final String PATH = "template/invoice_template.pdf";

    public IGTTemplatePDF(String templatePath) {

        try {
      
        	InputStream is = IGTTemplatePDF.class.getClassLoader().getResourceAsStream("template/invoice_template.pdf");
        	PdfReader pdfTemplate = new PdfReader(is);
        	PdfStamper stamper = new PdfStamper(pdfTemplate, os);
            stamper.setFormFlattening(true);
            stamper.getAcroFields().setField(ADDRESS, "Ashwin Kumar");
            stamper.close();
            pdfTemplate.close();
            
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            
        }
    }

    @Override
    public InputStream getStream() {
        // Here we return the pdf contents as a byte-array
        return new ByteArrayInputStream(os.toByteArray());
    }
}